import { Connector } from "cax-connector";
import { API_EVENT, IReq, IRes } from "../interface";
import { v4 as uuidv4 } from "uuid";
import { devtool } from "cax-base";
export function measure_point_to_point(
  this: Connector,
  Id1: number,
  Id2: number,
  PartId1: number,
  PartId2: number
) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.MEASURE_POINT_TO_POINT,
    Params: {
      Id1,
      Id2,
      PartId1,
      PartId2,
    },
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      devtool.success("measure_point_to_point", ev);
      if (ev.Status === 0) {
        resolve(ev.Result.Distance);
      } else {
        reject(ev.Errmsg);
      }
    });
  });
}
